package com.test.map;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.test.map.HTTP.OKHttp;
import com.test.map.HTTP.OkHttpUtil;
import com.test.map.sign.BaseActivity;
import com.test.map.sign.DateBaseHelper;

import java.util.ArrayList;

import okhttp3.FormBody;

public class Sign extends BaseActivity implements CompoundButton.OnCheckedChangeListener{
    private DateBaseHelper dphelper;
    private EditText a,b,c;
    private Button sign;
    private CheckBox[] checkBoxes = new CheckBox[2];
    public int type=0;
    private OKHttp http= OkHttpUtil.getInstance();
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        dphelper=new DateBaseHelper(this,"Users.db",null,1);
        sign=(Button)findViewById(R.id.Sign);
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //dphelper.getWritableDatabase();
                a=(EditText)findViewById(R.id.Sign_account);
                b=(EditText)findViewById(R.id.Sign_password) ;
                c = (EditText) findViewById(R.id.Sign_phone);
                String newname =a.getText().toString();
                String password=b.getText().toString();
                String phone=c.getText().toString();
                //Log.d("Zhuche","sss");


                if (CheckIsDataAlreadyInDBorNot(phone)) {
                    Toast.makeText(Sign.this,"该用户名已被注册，注册失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    if (register(phone, password,type)) {
                        if(type==1){
                            ArrayList<String> info = new ArrayList<String>();
                            info.add(newname);
                            info.add(password);
                            info.add(phone);
                            Toast.makeText(Sign.this, "恭喜您 注册成功，即将进入车辆注册界面", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(Sign.this,car_sign.class);
                            intent.putStringArrayListExtra("name", info);
                            startActivity(intent);
                        }else {
                            FormBody body = new FormBody.Builder()
                                    .add("name",newname)
                                    .add("account",phone)
                                    .add("password",password)
                                    .build();
                            http.post("http://47.105.179.182:5000/register/user",body);
                            Toast.makeText(Sign.this, "恭喜您 注册成功，即将返回登录界面", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(Sign.this,MainActivity.class);
                            startActivity(intent);
                        }
                    }}
            }
        });
        checkBoxes[0] = (CheckBox) findViewById(R.id.guest);
        checkBoxes[1] = (CheckBox) findViewById(R.id.driver);
        checkBoxes[0].setOnCheckedChangeListener( this);
        checkBoxes[1].setOnCheckedChangeListener( this);
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b) {
            for (int i = 0; i < checkBoxes.length; i++) {
                //不等于当前选中的就变成false
                if (checkBoxes[i].getText().toString().equals(compoundButton.getText().toString())) {
                    checkBoxes[i].setChecked(true);
                    type=i;
                } else {
                    checkBoxes[i].setChecked(false);
                }
            }
        }
    }

    //向数据库插入数据
    public boolean register(String username,String password,int type){
        SQLiteDatabase db= dphelper.getWritableDatabase();
        /*String sql = "insert into userData(name,password) value(?,?)";
        Object obj[]={username,password};
        db.execSQL(sql,obj);*/
        ContentValues values=new ContentValues();
        values.put("id",username);
        values.put("password",password);
        values.put("type",Integer.toString(type));
        db.insert("userData",null,values);
        db.close();
        //db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
        return true;

    }

    //检验用户名是否已存在

    public boolean CheckIsDataAlreadyInDBorNot(String value){
        SQLiteDatabase db=dphelper.getWritableDatabase();
        String Query = "Select * from userData where id =?";
        Cursor cursor = db.rawQuery(Query,new String[] { value });
        if (cursor.getCount()>0){
            cursor.close();
            return  true;
        }
        cursor.close();
        return false;
    }
}


